package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiCarrierExtEditBo;
import com.piggy.logi.sys.bo.LogiCarrierQueryBo;
import com.piggy.logi.sys.service.ILogiCarrierService;
import com.piggy.logi.sys.vo.LogiCarrierExtVo;
import com.piggy.logi.sys.vo.LogiCarrierVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 承运商信息Controller
 */
@Api(value = "承运商信息控制器", tags = {"承运商信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/logi/carrier")
public class LogiCarrierController extends BaseController {

    private final ILogiCarrierService iLogiCarrierService;

    /**
     * 查询承运商信息列表
     */
    @ApiOperation("查询承运商信息列表")
    //@SaCheckPermission("@ss.hasPermi('sys:carrier:list')")
    @PostMapping("/list")
    public TableDataInfo<LogiCarrierExtVo> list(@Validated @RequestBody LogiCarrierQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiCarrierService.queryPageList(bo);
    }

    /**
     * 导出承运商信息列表
     */
    @ApiOperation("导出承运商信息列表")
    //@SaCheckPermission("@ss.hasPermi('sys:carrier:export')")
    @LogiLog(title = "承运商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated @RequestBody LogiCarrierQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiCarrierExtVo> list = iLogiCarrierService.queryList(bo);
        ExcelUtil<LogiCarrierExtVo> util = new ExcelUtil<>(LogiCarrierExtVo.class);
        util.exportExcel(response, list, "承运商信息");
    }

    /**
     * 获取承运商信息详细信息
     */
    @ApiOperation("获取承运商信息详细信息")
    //@SaCheckPermission("@ss.hasPermi('sys:carrier:query')")
    @GetMapping("/{id}")
    public R<LogiCarrierVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(iLogiCarrierService.queryById(id));
    }

    /**
     * 新增承运商信息
     */
    @ApiOperation("新增承运商信息")
    //@SaCheckPermission("@ss.hasPermi('sys:carrier:add')")
    @LogiLog(title = "承运商信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Integer> add(@Validated @RequestBody LogiCarrierExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiCarrierService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改承运商信息
     */
    @ApiOperation("修改承运商信息")
    //@SaCheckPermission("@ss.hasPermi('sys:carrier:edit')")
    @LogiLog(title = "承运商信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Integer> edit(@Validated @RequestBody LogiCarrierExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiCarrierService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除承运商信息
     */
    @ApiOperation("删除承运商信息")
    //@SaCheckPermission("@ss.hasPermi('sys:carrier:remove')")
    @LogiLog(title = "承运商信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                             @PathVariable Long[] ids) {
        return R.ok(iLogiCarrierService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
